package org.gaojianqiao.electronichandbook.database;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.gaojianqiao.electronichandbook.createtable.SearchHistoryTab;
import org.gaojianqiao.electronichandbook.createtable.TreeView;

import zyg.login.DataBas;

public class OperateTreeView
{
	public ArrayList<TreeView> Treelist(String bookName) throws SQLException,
			UnsupportedEncodingException
	{

		// TODO Auto-generated method stub

		String sql = "select * from " + bookName + " order by id";
		DataBas publicDataBase = new DataBas();
		publicDataBase.connectDB("data3");
		ResultSet rs = publicDataBase.stmt.executeQuery(sql);
		ArrayList<TreeView> Treelist = new ArrayList<TreeView>();
		while (rs.next())
		{
			TreeView Tree = new TreeView();
			Tree.setid(rs.getString("id"));
			Tree.settitle(rs.getString("title"));
			Tree.setmhasparent(rs.getString("mhasparent"));
			Tree.setmhaschild(rs.getString("mhaschild"));
			Tree.setparent(rs.getString("parent"));
			Tree.setlevel(rs.getString("level"));
			Tree.setexpanded(rs.getString("expanded"));
			Treelist.add(Tree);
			// System.out.println(""+rs.getString("weapon_id")+rs.getString("weapon_title")+rs.getString("Tree"));
		}
		publicDataBase.closeDB();

		return Treelist;
	}

	public void insert(ArrayList<TreeView> str, String tablename)
			throws SQLException, ClassNotFoundException
	{
		String sql = "insert into " + tablename + " values (?,?)";
		DataBas publicDataBase = new DataBas();
		Connection conn = publicDataBase.connectDB("public");
		PreparedStatement ps = conn.prepareStatement(sql);
		int i = 0;
		do
		{
			ps.setString(1, str.get(i).getid());
			ps.setString(2, str.get(i).gettitle());
			ps.setString(3, str.get(i).getmhasparent());
			ps.setString(4, str.get(i).getmhaschild());
			ps.setString(5, str.get(i).getparent());
			ps.setString(6, str.get(i).getexpanded());
			ps.executeUpdate();
			++i;
		} while (i < str.size());
		ps.close();
		conn.close();

	}

}
